package ControlLayer;

import java.util.ArrayList;
import java.util.Date;

import ModelLayer.*;

import DBLayer.*;

public class ActivityBookingCtr {
	IFDBActivityBooking db;

	public ActivityBookingCtr() {
		db = new DBActivityBooking();
	}

	public ActivityBooking getActivityBooking(long ID) {
		return db.getActivityBooking(ID);
	}

	public ArrayList<ActivityBooking> getAllActivityBookings() {
		return db.getAllActivityBookings();
	}

	public ArrayList<ActivityBooking> getAllActivityBookings(Date date) {
		ArrayList<ActivityBooking> list = new ArrayList<>();
		ArrayList<ActivityBooking> fullList = getAllActivityBookings();

		for (ActivityBooking a : fullList) {
			if (a.getStartHour().getTime() == date.getTime()) {
				list.add(a);
			}
		}

		return list;
	}

	public ArrayList<ActivityBooking> getAllActivityBookings(Facility f) {
		ArrayList<ActivityBooking> list = new ArrayList<>();
		ArrayList<ActivityBooking> fullList = getAllActivityBookings();

		for (ActivityBooking a : fullList) {
			if (a.getFacility().getID() == f.getID()) {
				list.add(a);
			}
		}

		return list;
	}

	public ArrayList<ActivityBooking> getAllActivityBookings(ActivityType at,
			Date date) {
		ArrayList<ActivityBooking> list = new ArrayList<>();
		ArrayList<ActivityBooking> fullList = getAllActivityBookings();

		for (ActivityBooking a : fullList) {
			if (a.getStartHour().getTime() == date.getTime()
					&& a.getActivity().getID() == at.getID()) {
				list.add(a);
			}
		}

		return list;
	}

	public ArrayList<ActivityBooking> getAllActivityBookings(ActivityType at,
			Facility f) {
		ArrayList<ActivityBooking> list = new ArrayList<>();
		ArrayList<ActivityBooking> fullList = getAllActivityBookings();

		for (ActivityBooking a : fullList) {
			if (a.getActivity().getID() == at.getID()
					&& a.getFacility().getID() == f.getID()) {
				list.add(a);
			}
		}

		return list;
	}

	public boolean isFacilityBooked(Facility f, Date date) {
		boolean isBooked = false;

		ArrayList<ActivityBooking> fullList = getAllActivityBookings();

		for (ActivityBooking a : fullList) {
			if (a.getFacility().getID() == f.getID()
					&& a.getStartHour().getTime() == date.getTime()) {
				isBooked = true;
			}
		}

		return isBooked;
	}

	public ArrayList<Facility> getAvailableFacilities(Date date, ActivityType at) {
		ArrayList<Facility> list = new ArrayList<>();
		ArrayList<Facility> flist = new FacilityCtr().getFacilities(at);

		for (Facility f : flist) {
			if (!isFacilityBooked(f, date))
				list.add(f);
		}

		return list;
	}

	public int insertActivityBooking(ActivityBooking ab) {
		return db.insertActivityBooking(ab);
	}

	public int updateActivityBooking(ActivityBooking ab) {
		return db.updateActivityBooking(ab.getID(), ab);
	}

	public int removeActivityBooking(ActivityBooking ab) {
		return db.removeActivityBooking(ab.getID());
	}

	public int removeActivityBooking(long ID) {
		return db.removeActivityBooking(ID);
	}

	public void createTempLock(ActivityType activity) {

	}

	public void createTempLock(ActivityType a, Facility f) {

	}
}
